/*
 This file is part of WebHUD.
 
 WebHUD is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 any later version.
 
 WebHUD is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with WebHUD.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "WHHelper.h"
#import "Constants.h"
#import "WHController.h"
#import <Security/Security.h>
#import <objc/objc-class.h>



@implementation WHPaths

+ (NSString *)applicationSupportFolderPath
{
	return [ShortApplicationSupportFolderPath stringByExpandingTildeInPath];
}

@end



@implementation NSObject (Swizzle)

+ (BOOL)swizzleMethod:(SEL)old withMethod:(SEL)new
{
	Method oldMethod = class_getInstanceMethod(self, old);
	if (!oldMethod)
		return NO;
	
	Method newMethod = class_getInstanceMethod(self, new);
	if (!newMethod)
		return NO;
	
	method_exchangeImplementations(oldMethod, newMethod);
	return YES;
}

+ (BOOL)overrideMethod:(NSString *)methodName
{
	return [self swizzleMethod:NSSelectorFromString(methodName)
					withMethod:NSSelectorFromString([@"_" stringByAppendingString:methodName])];
}

+ (BOOL)overrideMethods:(NSArray *)methods
{
	for (NSString *method in methods)
		if (![self overrideMethod:method])
			return NO;
	return YES;
}

@end


void swizzle(Class originalClass, SEL originalName, Class newClass, SEL newName)
{
	method_exchangeImplementations(class_getInstanceMethod(originalClass, originalName), class_getInstanceMethod(newClass, newName));
}